<?php
/* SVN FILE: $Id: troops_controller.php 17 2009-04-20 02:00:59Z ake.svedin $ */
/**
 * Travian Alliance Coordinator
 * Copyright (c) 2009 Åke Svedin.
 *
 * This file is part of Travian Alliance Coordinator.
 *
 * Travian Alliance Coordinator is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Travian Alliance Coordinator is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Travian Alliance Coordinator.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author $Author: ake.svedin $
 * @copyright Copyright (c) 2009 Åke Svedin.
 * @version $Revision: 17 $
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3
 */
class TroopsController extends AppController {

	var $name = 'Troops';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Troop->recursive = 0;
		$this->set('troops', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Troop.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('troop', $this->Troop->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Troop->create();
			if ($this->Troop->save($this->data)) {
				$this->Session->setFlash(__('The Troop has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Troop could not be saved. Please, try again.', true));
			}
		}
		$villages = $this->Troop->Village->find('list');
		$this->set(compact('villages'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Troop', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Troop->save($this->data)) {
				$this->Session->setFlash(__('The Troop has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Troop could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Troop->read(null, $id);
		}
		$villages = $this->Troop->Village->find('list');
		$this->set(compact('villages'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Troop', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Troop->del($id)) {
			$this->Session->setFlash(__('Troop deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>